package com.edgclub.edg.activity;

import android.content.Intent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.ali.auth.third.ui.context.CallbackContext;
import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.model.ResultType;
import com.alibaba.baichuan.android.trade.model.TradeResult;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcDetailPage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.edgclub.edg.R;
import com.edgclub.edg.utils.util.UserPrefUtils;

import java.util.HashMap;
import java.util.Map;


public class GzWebViewActivityTaobao extends GzBaseTooolBarActivity implements
        View.OnClickListener {
    WebView webView = null;
    private String opneUrl="";
    private UserPrefUtils userPrefUtil;

    @Override
    public int getContentView() {
        return R.layout.activity_webview_layout;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("EDG官方商城");
    }

    @Override
    public void initView() {
        userPrefUtil = new UserPrefUtils(this);
        opneUrl=getIntent().getStringExtra("url");
        webView = (WebView) this.findViewById(R.id.webview);
        WebSettings settings = webView.getSettings();
//        // 自适应屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setJavaScriptEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);

        webView.getSettings().setJavaScriptEnabled(true);

        showDeatil();

    }

    public void showDeatil() {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");

        //实例化商品详情page
        AlibcBasePage aliTradeBasePage = new AlibcPage(opneUrl);
        AlibcShowParams alibcShowParams = new AlibcShowParams(OpenType.H5, false);
        AlibcTrade.show(this, webView,null , null ,aliTradeBasePage, alibcShowParams, null, exParams , new AlibcTradeCallback() {
            @Override
            public void onTradeSuccess(TradeResult tradeResult) {
                if(tradeResult.resultType.equals(ResultType.TYPECART)){
                    //加购成功
                    Toast.makeText(GzWebViewActivityTaobao.this, "加购成功", Toast.LENGTH_SHORT).show();
                }else if (tradeResult.resultType.equals(ResultType.TYPEPAY)){
                    //支付成功
                    Toast.makeText(GzWebViewActivityTaobao.this, "支付成功,成功订单号为"+tradeResult.payResult.paySuccessOrders, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                Toast.makeText(GzWebViewActivityTaobao.this, "流程出错,错误码="+errCode+" ,错误信息="+errMsg, Toast.LENGTH_SHORT).show();
                //打开电商组件，用户操作中错误信息回调。code：错误码；msg：错误信息
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //上报onActivityResult，如果遗漏，会出现调用逻辑错误；
        CallbackContext.onActivityResult(requestCode, resultCode, data);
    }

    //如果使用自定义webview的时候，需要在activity加上以下代码
    @Override
    public void onBackPressed() {
        if(webView.canGoBack()){
            webView.goBack();
        }else {
            finish();
        }
    }

    @Override
    public void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_refresh1:
                webView.reload();
                break;
        }
    }

}
